package com.lvmama.pangolin.webmagic.util;

import java.util.Date;
import java.util.List;
import org.apache.http.HttpHost;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.lvmama.pangolin.common.DateUtil;
import us.codecraft.webmagic.Site;
@Component
public class SiteUtil{
	
	/**   
	* @Title: SiteUtil.java  
	* @Description: TODO(用一句话描述该文件做什么) 
	* @author ZhouTianQi   
	* @date 2017年1月4日 上午10:24:10 
	* @version V1.0   
	*/
	private static Logger logger = LoggerFactory.getLogger(SiteUtil.class);
	private static List<String> hostList;

	@SuppressWarnings("static-access")
	public Site getSite(){
		Site site = null;
		String hostname = "";
		int port = 80;
		try {
			if(this.hostList == null && this.hostList.size()<=0){
				//这里增加从数据库获取IP逻辑
				//IP集合存入上下文
			}
			hostname=this.hostList.get(0).split(":")[0];
			port=Integer.parseInt(this.hostList.get(0).split(":")[1]);
			HttpHost httpHost = new HttpHost(hostname, port,"");
			//me() will create a new site every time
			site = Site.me().setRetryTimes(3).setSleepTime(1000).setTimeOut(10000).setHttpProxy(httpHost);
		} catch (Exception e) {
			site = Site.me().setRetryTimes(3).setSleepTime(1000).setTimeOut(10000);
			//e.printStackTrace();
		}
		
		return site;
	}
	//IP不可用  生成新的Site
	@SuppressWarnings("static-access")
	public Site getAnotherSite(String errorIP,String company,Date date){
		logger.info("------------"+DateUtil.formatDate(date, "yyyy:MM:dd hh:mm:ss")+"#####Info:"+company+errorIP);
		try {
			this.hostList.remove(errorIP);
			//errorIP入黑名单逻辑
			return getSite();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public void getIPS(){
		//数据库读取
	}
}
